{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "import torch\n",
    "import colorsys\n",
    "import numpy as np\n",
    "import torch.nn as nn\n",
    "from torch.autograd import Variable\n",
    "import torch.backends.cudnn as cudnn\n",
    "from yolo import YOLO\n",
    "from nets.yolo4 import YoloBody\n",
    "import xml.etree.ElementTree as ET\n",
    "from PIL import Image, ImageFont, ImageDraw\n",
    "from utils.utils import non_max_suppression, bbox_iou, DecodeBox,letterbox_image,yolo_correct_boxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ground-truth annotations path\n",
    "annotation_path = 'datasets/val/Annotations'\n",
    "annotations = sorted(os.listdir(annotation_path))\n",
    "# test images path\n",
    "image_path = 'datasets/val/JPEGImages'\n",
    "images = sorted(os.listdir(image_path))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start making ground-truth!\n",
      "Finish making ground-truth!\n"
     ]
    }
   ],
   "source": [
    "# making ground-truth\n",
    "print('Start making ground-truth!')\n",
    "for annotation in annotations:\n",
    "    tree = ET.parse(os.path.join(annotation_path, annotation))\n",
    "    objects = tree.findall('object')\n",
    "    with open(os.path.join('mAP/input/ground-truth', annotation.split('.')[0] + '.txt'), 'w+') as f:\n",
    "        for obj in objects:\n",
    "            name = str(obj.find('name').text)\n",
    "            bndbox = obj.find('bndbox')\n",
    "            xmin = str(bndbox.find('xmin').text)\n",
    "            ymin = str(bndbox.find('ymin').text)\n",
    "            xmax = str(bndbox.find('xmax').text)\n",
    "            ymax = str(bndbox.find('ymax').text)\n",
    "            f.write(' '.join((name, xmin, ymin, xmax, ymax)) + '\\n')\n",
    "print('Finish making ground-truth!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Start making detection results!\n",
      "Loading pretrained weights.\n",
      "Finish loading!\n",
      "model_data/yolov4_maskdetect_weights1.pth model, anchors, and classes loaded.\n",
      "(1, 4)\n",
      "[done] val_1.jpg\n",
      "(1, 4)\n",
      "[done] val_10.jpg\n",
      "(1, 4)\n",
      "[done] val_100.jpg\n",
      "(1, 4)\n",
      "[done] val_101.jpg\n",
      "(1, 4)\n",
      "[done] val_102.jpg\n",
      "(1, 4)\n",
      "[done] val_103.jpg\n",
      "(1, 4)\n",
      "[done] val_104.jpg\n",
      "(1, 4)\n",
      "[done] val_105.jpg\n",
      "(1, 4)\n",
      "[done] val_106.jpg\n",
      "(2, 4)\n",
      "[done] val_107.jpg\n",
      "(3, 4)\n",
      "[done] val_108.jpg\n",
      "(1, 4)\n",
      "[done] val_109.jpg\n",
      "(1, 4)\n",
      "[done] val_11.jpg\n",
      "(1, 4)\n",
      "[done] val_110.jpg\n",
      "(1, 4)\n",
      "[done] val_111.jpg\n",
      "(1, 4)\n",
      "[done] val_112.jpg\n",
      "(1, 4)\n",
      "[done] val_113.jpg\n",
      "(1, 4)\n",
      "[done] val_114.jpg\n",
      "(1, 4)\n",
      "[done] val_115.jpg\n",
      "(1, 4)\n",
      "[done] val_116.jpg\n",
      "(1, 4)\n",
      "[done] val_117.jpg\n",
      "(1, 4)\n",
      "[done] val_118.jpg\n",
      "(1, 4)\n",
      "[done] val_119.jpg\n",
      "(1, 4)\n",
      "[done] val_12.jpg\n",
      "(1, 4)\n",
      "[done] val_120.jpg\n",
      "(1, 4)\n",
      "[done] val_121.jpg\n",
      "(1, 4)\n",
      "[done] val_122.jpg\n",
      "(1, 4)\n",
      "[done] val_123.jpg\n",
      "(1, 4)\n",
      "[done] val_124.jpg\n",
      "(1, 4)\n",
      "[done] val_125.jpg\n",
      "(1, 4)\n",
      "[done] val_126.jpg\n",
      "(1, 4)\n",
      "[done] val_127.jpg\n",
      "(1, 4)\n",
      "[done] val_128.jpg\n",
      "(1, 4)\n",
      "[done] val_129.jpg\n",
      "(1, 4)\n",
      "[done] val_13.jpg\n",
      "(1, 4)\n",
      "[done] val_130.jpg\n",
      "(1, 4)\n",
      "[done] val_131.jpg\n",
      "(1, 4)\n",
      "[done] val_132.jpg\n",
      "(1, 4)\n",
      "[done] val_133.jpg\n",
      "(1, 4)\n",
      "[done] val_134.jpg\n",
      "(1, 4)\n",
      "[done] val_135.jpg\n",
      "(1, 4)\n",
      "[done] val_136.jpg\n",
      "(1, 4)\n",
      "[done] val_137.jpg\n",
      "(1, 4)\n",
      "[done] val_138.jpg\n",
      "(4, 4)\n",
      "[done] val_139.jpg\n",
      "(1, 4)\n",
      "[done] val_14.jpg\n",
      "(1, 4)\n",
      "[done] val_140.jpg\n",
      "(1, 4)\n",
      "[done] val_141.jpg\n",
      "(5, 4)\n",
      "[done] val_142.jpg\n",
      "(2, 4)\n",
      "[done] val_143.jpg\n",
      "(7, 4)\n",
      "[done] val_144.jpg\n",
      "(4, 4)\n",
      "[done] val_145.jpg\n",
      "(4, 4)\n",
      "[done] val_146.jpg\n",
      "(7, 4)\n",
      "[done] val_147.jpg\n",
      "(2, 4)\n",
      "[done] val_148.jpg\n",
      "(7, 4)\n",
      "[done] val_149.jpg\n",
      "(1, 4)\n",
      "[done] val_15.jpg\n",
      "(2, 4)\n",
      "[done] val_150.jpg\n",
      "(1, 4)\n",
      "[done] val_151.jpg\n",
      "(7, 4)\n",
      "[done] val_152.jpg\n",
      "(5, 4)\n",
      "[done] val_153.jpg\n",
      "(1, 4)\n",
      "[done] val_154.jpg\n",
      "(1, 4)\n",
      "[done] val_155.jpg\n",
      "(1, 4)\n",
      "[done] val_156.jpg\n",
      "(6, 4)\n",
      "[done] val_157.jpg\n",
      "(1, 4)\n",
      "[done] val_158.jpg\n",
      "(15, 4)\n",
      "[done] val_159.png\n",
      "(5, 4)\n",
      "[done] val_16.jpg\n",
      "(2, 4)\n",
      "[done] val_160.jpg\n",
      "(1, 4)\n",
      "[done] val_161.jpg\n",
      "(3, 4)\n",
      "[done] val_162.jpg\n",
      "(2, 4)\n",
      "[done] val_163.jpg\n",
      "(1, 4)\n",
      "[done] val_164.jpg\n",
      "(1, 4)\n",
      "[done] val_165.jpg\n",
      "(13, 4)\n",
      "[done] val_166.jpg\n",
      "(1, 4)\n",
      "[done] val_167.jpg\n",
      "(5, 4)\n",
      "[done] val_168.jpg\n",
      "(11, 4)\n",
      "[done] val_169.jpg\n",
      "(4, 4)\n",
      "[done] val_17.jpg\n",
      "(3, 4)\n",
      "[done] val_170.jpg\n",
      "(5, 4)\n",
      "[done] val_171.jpg\n",
      "(1, 4)\n",
      "[done] val_172.jpg\n",
      "(1, 4)\n",
      "[done] val_173.jpg\n",
      "(1, 4)\n",
      "[done] val_174.jpg\n",
      "(4, 4)\n",
      "[done] val_175.jpg\n",
      "(9, 4)\n",
      "[done] val_176.jpg\n",
      "(5, 4)\n",
      "[done] val_177.jpg\n",
      "(7, 4)\n",
      "[done] val_178.jpg\n",
      "(6, 4)\n",
      "[done] val_179.jpg\n",
      "(1, 4)\n",
      "[done] val_18.jpg\n",
      "(2, 4)\n",
      "[done] val_180.jpg\n",
      "(8, 4)\n",
      "[done] val_181.png\n",
      "(7, 4)\n",
      "[done] val_182.png\n",
      "(6, 4)\n",
      "[done] val_183.png\n",
      "(3, 4)\n",
      "[done] val_184.png\n",
      "(5, 4)\n",
      "[done] val_185.png\n",
      "(9, 4)\n",
      "[done] val_186.jpg\n",
      "(13, 4)\n",
      "[done] val_187.jpg\n",
      "(20, 4)\n",
      "[done] val_188.jpg\n",
      "(6, 4)\n",
      "[done] val_189.jpg\n",
      "(1, 4)\n",
      "[done] val_19.jpg\n",
      "(11, 4)\n",
      "[done] val_190.jpg\n",
      "(1, 4)\n",
      "[done] val_191.jpg\n",
      "(1, 4)\n",
      "[done] val_192.jpg\n",
      "(1, 4)\n",
      "[done] val_193.jpg\n",
      "(1, 4)\n",
      "[done] val_194.jpg\n",
      "(1, 4)\n",
      "[done] val_195.jpg\n",
      "(1, 4)\n",
      "[done] val_196.jpg\n",
      "(1, 4)\n",
      "[done] val_197.jpg\n",
      "(2, 4)\n",
      "[done] val_198.jpg\n",
      "(1, 4)\n",
      "[done] val_199.jpg\n",
      "(1, 4)\n",
      "[done] val_2.jpg\n",
      "(2, 4)\n",
      "[done] val_20.jpg\n",
      "(1, 4)\n",
      "[done] val_200.jpg\n",
      "(1, 4)\n",
      "[done] val_201.jpg\n",
      "(1, 4)\n",
      "[done] val_202.jpg\n",
      "(2, 4)\n",
      "[done] val_203.jpg\n",
      "(1, 4)\n",
      "[done] val_204.jpg\n",
      "(1, 4)\n",
      "[done] val_205.jpg\n",
      "(5, 4)\n",
      "[done] val_206.jpg\n",
      "(1, 4)\n",
      "[done] val_207.jpg\n",
      "(1, 4)\n",
      "[done] val_208.jpg\n",
      "(1, 4)\n",
      "[done] val_209.jpg\n",
      "(1, 4)\n",
      "[done] val_21.jpg\n",
      "(1, 4)\n",
      "[done] val_210.jpg\n",
      "(4, 4)\n",
      "[done] val_211.jpg\n",
      "(5, 4)\n",
      "[done] val_212.jpg\n",
      "(9, 4)\n",
      "[done] val_213.jpg\n",
      "(1, 4)\n",
      "[done] val_214.jpg\n",
      "(1, 4)\n",
      "[done] val_215.jpg\n",
      "(2, 4)\n",
      "[done] val_216.jpg\n",
      "(3, 4)\n",
      "[done] val_217.jpg\n",
      "(6, 4)\n",
      "[done] val_218.jpg\n",
      "(2, 4)\n",
      "[done] val_219.jpg\n",
      "(1, 4)\n",
      "[done] val_22.jpg\n",
      "(2, 4)\n",
      "[done] val_220.jpg\n",
      "(6, 4)\n",
      "[done] val_221.jpg\n",
      "(4, 4)\n",
      "[done] val_222.jpg\n",
      "(6, 4)\n",
      "[done] val_223.jpg\n",
      "(6, 4)\n",
      "[done] val_224.jpg\n",
      "(11, 4)\n",
      "[done] val_225.jpg\n",
      "(6, 4)\n",
      "[done] val_226.jpg\n",
      "(5, 4)\n",
      "[done] val_227.jpg\n",
      "(5, 4)\n",
      "[done] val_228.jpg\n",
      "(10, 4)\n",
      "[done] val_229.jpg\n",
      "(1, 4)\n",
      "[done] val_23.jpg\n",
      "(2, 4)\n",
      "[done] val_230.jpg\n",
      "(1, 4)\n",
      "[done] val_231.jpg\n",
      "(3, 4)\n",
      "[done] val_232.jpg\n",
      "(3, 4)\n",
      "[done] val_233.jpg\n",
      "(9, 4)\n",
      "[done] val_234.jpg\n",
      "(5, 4)\n",
      "[done] val_235.jpg\n",
      "(2, 4)\n",
      "[done] val_236.jpg\n",
      "(6, 4)\n",
      "[done] val_237.jpg\n",
      "(4, 4)\n",
      "[done] val_238.jpg\n",
      "(5, 4)\n",
      "[done] val_239.jpg\n",
      "(1, 4)\n",
      "[done] val_24.jpg\n",
      "(5, 4)\n",
      "[done] val_240.jpg\n",
      "(3, 4)\n",
      "[done] val_241.jpg\n",
      "(8, 4)\n",
      "[done] val_242.jpg\n",
      "(3, 4)\n",
      "[done] val_243.jpg\n",
      "(5, 4)\n",
      "[done] val_244.jpg\n",
      "(5, 4)\n",
      "[done] val_245.jpg\n",
      "(9, 4)\n",
      "[done] val_246.jpg\n",
      "(5, 4)\n",
      "[done] val_247.jpg\n",
      "(5, 4)\n",
      "[done] val_248.jpg\n",
      "(2, 4)\n",
      "[done] val_249.jpg\n",
      "(1, 4)\n",
      "[done] val_25.jpg\n",
      "(6, 4)\n",
      "[done] val_250.jpg\n",
      "(10, 4)\n",
      "[done] val_251.jpg\n",
      "(10, 4)\n",
      "[done] val_252.jpg\n",
      "(15, 4)\n",
      "[done] val_253.jpg\n",
      "(2, 4)\n",
      "[done] val_254.jpg\n",
      "(4, 4)\n",
      "[done] val_255.jpg\n",
      "(1, 4)\n",
      "[done] val_256.jpg\n",
      "(5, 4)\n",
      "[done] val_257.jpg\n",
      "(6, 4)\n",
      "[done] val_258.jpg\n",
      "(4, 4)\n",
      "[done] val_259.jpg\n",
      "(1, 4)\n",
      "[done] val_26.jpg\n",
      "(10, 4)\n",
      "[done] val_260.jpg\n",
      "(3, 4)\n",
      "[done] val_261.jpg\n",
      "(5, 4)\n",
      "[done] val_262.jpg\n",
      "(7, 4)\n",
      "[done] val_263.jpg\n",
      "(7, 4)\n",
      "[done] val_264.jpg\n",
      "(6, 4)\n",
      "[done] val_265.jpg\n",
      "(9, 4)\n",
      "[done] val_266.jpg\n",
      "(3, 4)\n",
      "[done] val_267.jpg\n",
      "(10, 4)\n",
      "[done] val_268.jpg\n",
      "(16, 4)\n",
      "[done] val_269.jpg\n",
      "(1, 4)\n",
      "[done] val_27.jpg\n",
      "(6, 4)\n",
      "[done] val_270.jpg\n",
      "(55, 4)\n",
      "[done] val_271.jpg\n",
      "(13, 4)\n",
      "[done] val_272.jpg\n",
      "(1, 4)\n",
      "[done] val_273.jpg\n",
      "(1, 4)\n",
      "[done] val_274.jpg\n",
      "(2, 4)\n",
      "[done] val_275.jpg\n",
      "(1, 4)\n",
      "[done] val_276.jpg\n",
      "(8, 4)\n",
      "[done] val_277.jpg\n",
      "(1, 4)\n",
      "[done] val_278.jpg\n",
      "(1, 4)\n",
      "[done] val_279.jpg\n",
      "(3, 4)\n",
      "[done] val_28.jpg\n",
      "(6, 4)\n",
      "[done] val_280.jpg\n",
      "(5, 4)\n",
      "[done] val_281.jpg\n",
      "(3, 4)\n",
      "[done] val_282.jpg\n",
      "(5, 4)\n",
      "[done] val_283.jpg\n",
      "(5, 4)\n",
      "[done] val_284.jpg\n",
      "(1, 4)\n",
      "[done] val_285.jpg\n",
      "(4, 4)\n",
      "[done] val_286.jpg\n",
      "(4, 4)\n",
      "[done] val_287.jpg\n",
      "(2, 4)\n",
      "[done] val_288.jpg\n",
      "(5, 4)\n",
      "[done] val_289.jpg\n",
      "(1, 4)\n",
      "[done] val_29.jpg\n",
      "(2, 4)\n",
      "[done] val_290.jpg\n",
      "(2, 4)\n",
      "[done] val_291.jpg\n",
      "(1, 4)\n",
      "[done] val_292.jpg\n",
      "(3, 4)\n",
      "[done] val_293.jpg\n",
      "(4, 4)\n",
      "[done] val_294.jpg\n",
      "(4, 4)\n",
      "[done] val_295.jpg\n",
      "(7, 4)\n",
      "[done] val_296.jpg\n",
      "(7, 4)\n",
      "[done] val_297.jpg\n",
      "(3, 4)\n",
      "[done] val_298.jpg\n",
      "(4, 4)\n",
      "[done] val_299.jpg\n",
      "(2, 4)\n",
      "[done] val_3.jpg\n",
      "(1, 4)\n",
      "[done] val_30.jpg\n",
      "(6, 4)\n",
      "[done] val_300.jpg\n",
      "(6, 4)\n",
      "[done] val_301.jpg\n",
      "(8, 4)\n",
      "[done] val_302.jpg\n",
      "(1, 4)\n",
      "[done] val_303.jpg\n",
      "(7, 4)\n",
      "[done] val_304.jpg\n",
      "(4, 4)\n",
      "[done] val_305.jpg\n",
      "(8, 4)\n",
      "[done] val_306.jpg\n",
      "(6, 4)\n",
      "[done] val_307.jpg\n",
      "(3, 4)\n",
      "[done] val_308.jpg\n",
      "(25, 4)\n",
      "[done] val_309.jpg\n",
      "(1, 4)\n",
      "[done] val_31.jpg\n",
      "(1, 4)\n",
      "[done] val_310.jpg\n",
      "(4, 4)\n",
      "[done] val_311.jpg\n",
      "(4, 4)\n",
      "[done] val_312.jpg\n",
      "(19, 4)\n",
      "[done] val_313.jpg\n",
      "(14, 4)\n",
      "[done] val_314.jpg\n",
      "(12, 4)\n",
      "[done] val_315.jpg\n",
      "(4, 4)\n",
      "[done] val_316.jpg\n",
      "(2, 4)\n",
      "[done] val_317.jpg\n",
      "(1, 4)\n",
      "[done] val_318.jpg\n",
      "(2, 4)\n",
      "[done] val_319.jpg\n",
      "(1, 4)\n",
      "[done] val_32.jpg\n",
      "(16, 4)\n",
      "[done] val_320.jpg\n",
      "(7, 4)\n",
      "[done] val_321.jpg\n",
      "(5, 4)\n",
      "[done] val_322.jpg\n",
      "(5, 4)\n",
      "[done] val_323.jpg\n",
      "(4, 4)\n",
      "[done] val_324.jpg\n",
      "(2, 4)\n",
      "[done] val_325.jpg\n",
      "(3, 4)\n",
      "[done] val_326.jpg\n",
      "(14, 4)\n",
      "[done] val_327.jpg\n",
      "(5, 4)\n",
      "[done] val_328.jpg\n",
      "(8, 4)\n",
      "[done] val_329.jpg\n",
      "(3, 4)\n",
      "[done] val_33.jpg\n",
      "(3, 4)\n",
      "[done] val_330.jpg\n",
      "(4, 4)\n",
      "[done] val_331.jpg\n",
      "(3, 4)\n",
      "[done] val_332.jpg\n",
      "(3, 4)\n",
      "[done] val_333.jpg\n",
      "(4, 4)\n",
      "[done] val_334.jpg\n",
      "(11, 4)\n",
      "[done] val_335.jpg\n",
      "(2, 4)\n",
      "[done] val_336.jpg\n",
      "(10, 4)\n",
      "[done] val_337.jpg\n",
      "(1, 4)\n",
      "[done] val_338.jpg\n",
      "(1, 4)\n",
      "[done] val_339.jpg\n",
      "(1, 4)\n",
      "[done] val_34.jpg\n",
      "(1, 4)\n",
      "[done] val_340.jpg\n",
      "(1, 4)\n",
      "[done] val_341.jpg\n",
      "(3, 4)\n",
      "[done] val_342.jpg\n",
      "(4, 4)\n",
      "[done] val_343.jpg\n",
      "(1, 4)\n",
      "[done] val_344.jpg\n",
      "(5, 4)\n",
      "[done] val_345.jpg\n",
      "(5, 4)\n",
      "[done] val_346.jpg\n",
      "(5, 4)\n",
      "[done] val_347.jpg\n",
      "(1, 4)\n",
      "[done] val_348.jpg\n",
      "(17, 4)\n",
      "[done] val_349.jpg\n",
      "(2, 4)\n",
      "[done] val_35.jpg\n",
      "(1, 4)\n",
      "[done] val_350.jpg\n",
      "(1, 4)\n",
      "[done] val_351.jpg\n",
      "(1, 4)\n",
      "[done] val_352.jpg\n",
      "(4, 4)\n",
      "[done] val_353.jpg\n",
      "(4, 4)\n",
      "[done] val_354.jpg\n",
      "(1, 4)\n",
      "[done] val_355.jpg\n",
      "(7, 4)\n",
      "[done] val_356.jpg\n",
      "(4, 4)\n",
      "[done] val_357.jpg\n",
      "(5, 4)\n",
      "[done] val_358.jpg\n",
      "(5, 4)\n",
      "[done] val_359.jpg\n",
      "(3, 4)\n",
      "[done] val_36.jpg\n",
      "(8, 4)\n",
      "[done] val_360.jpg\n",
      "(5, 4)\n",
      "[done] val_361.jpg\n",
      "(10, 4)\n",
      "[done] val_362.jpg\n",
      "(13, 4)\n",
      "[done] val_363.jpg\n",
      "(10, 4)\n",
      "[done] val_364.jpg\n",
      "(7, 4)\n",
      "[done] val_365.jpg\n",
      "(13, 4)\n",
      "[done] val_366.jpg\n",
      "(3, 4)\n",
      "[done] val_367.jpg\n",
      "(4, 4)\n",
      "[done] val_368.jpg\n",
      "(10, 4)\n",
      "[done] val_369.jpg\n",
      "(1, 4)\n",
      "[done] val_37.jpg\n",
      "(6, 4)\n",
      "[done] val_370.jpg\n",
      "(1, 4)\n",
      "[done] val_371.jpg\n",
      "(1, 4)\n",
      "[done] val_372.jpg\n",
      "(1, 4)\n",
      "[done] val_373.jpg\n",
      "(1, 4)\n",
      "[done] val_374.jpg\n",
      "(6, 4)\n",
      "[done] val_375.jpg\n",
      "(3, 4)\n",
      "[done] val_376.jpg\n",
      "(2, 4)\n",
      "[done] val_377.jpg\n",
      "(2, 4)\n",
      "[done] val_378.jpg\n",
      "(6, 4)\n",
      "[done] val_379.jpg\n",
      "(1, 4)\n",
      "[done] val_38.jpg\n",
      "(3, 4)\n",
      "[done] val_380.jpg\n",
      "(5, 4)\n",
      "[done] val_381.jpg\n",
      "(7, 4)\n",
      "[done] val_382.jpg\n",
      "(2, 4)\n",
      "[done] val_383.jpg\n",
      "(6, 4)\n",
      "[done] val_384.jpg\n",
      "(11, 4)\n",
      "[done] val_385.jpg\n",
      "(14, 4)\n",
      "[done] val_386.jpg\n",
      "(7, 4)\n",
      "[done] val_387.jpg\n",
      "(4, 4)\n",
      "[done] val_388.jpg\n",
      "(15, 4)\n",
      "[done] val_389.jpg\n",
      "(1, 4)\n",
      "[done] val_39.jpg\n",
      "(3, 4)\n",
      "[done] val_390.jpg\n",
      "(21, 4)\n",
      "[done] val_391.jpg\n",
      "(1, 4)\n",
      "[done] val_392.jpg\n",
      "(2, 4)\n",
      "[done] val_393.jpg\n",
      "(8, 4)\n",
      "[done] val_394.jpg\n",
      "(3, 4)\n",
      "[done] val_395.jpg\n",
      "(13, 4)\n",
      "[done] val_396.jpg\n",
      "(30, 4)\n",
      "[done] val_397.jpg\n",
      "(3, 4)\n",
      "[done] val_398.jpg\n",
      "(2, 4)\n",
      "[done] val_399.jpg\n",
      "(1, 4)\n",
      "[done] val_4.jpg\n",
      "(1, 4)\n",
      "[done] val_40.jpg\n",
      "(1, 4)\n",
      "[done] val_400.jpg\n",
      "(1, 4)\n",
      "[done] val_41.jpg\n",
      "(1, 4)\n",
      "[done] val_42.jpg\n",
      "(1, 4)\n",
      "[done] val_43.jpg\n",
      "(1, 4)\n",
      "[done] val_44.jpg\n",
      "(1, 4)\n",
      "[done] val_45.jpg\n",
      "(1, 4)\n",
      "[done] val_46.jpg\n",
      "(1, 4)\n",
      "[done] val_47.jpg\n",
      "(2, 4)\n",
      "[done] val_48.jpg\n",
      "(1, 4)\n",
      "[done] val_49.jpg\n",
      "(1, 4)\n",
      "[done] val_5.jpg\n",
      "(1, 4)\n",
      "[done] val_50.jpg\n",
      "(1, 4)\n",
      "[done] val_51.jpg\n",
      "(1, 4)\n",
      "[done] val_52.jpg\n",
      "(1, 4)\n",
      "[done] val_53.jpg\n",
      "(1, 4)\n",
      "[done] val_54.jpg\n",
      "(1, 4)\n",
      "[done] val_55.jpg\n",
      "(1, 4)\n",
      "[done] val_56.jpg\n",
      "(1, 4)\n",
      "[done] val_57.jpg\n",
      "(1, 4)\n",
      "[done] val_58.jpg\n",
      "(1, 4)\n",
      "[done] val_59.jpg\n",
      "(1, 4)\n",
      "[done] val_6.jpg\n",
      "(1, 4)\n",
      "[done] val_60.jpg\n",
      "(1, 4)\n",
      "[done] val_61.jpg\n",
      "(1, 4)\n",
      "[done] val_62.jpg\n",
      "(1, 4)\n",
      "[done] val_63.jpg\n",
      "(1, 4)\n",
      "[done] val_64.jpg\n",
      "(1, 4)\n",
      "[done] val_65.jpg\n",
      "(1, 4)\n",
      "[done] val_66.jpg\n",
      "(1, 4)\n",
      "[done] val_67.jpg\n",
      "(1, 4)\n",
      "[done] val_68.jpg\n",
      "(1, 4)\n",
      "[done] val_69.jpg\n",
      "(1, 4)\n",
      "[done] val_7.jpg\n",
      "(1, 4)\n",
      "[done] val_70.jpg\n",
      "(1, 4)\n",
      "[done] val_71.jpg\n",
      "(1, 4)\n",
      "[done] val_72.jpg\n",
      "(1, 4)\n",
      "[done] val_73.jpg\n",
      "(1, 4)\n",
      "[done] val_74.jpg\n",
      "(1, 4)\n",
      "[done] val_75.jpg\n",
      "(1, 4)\n",
      "[done] val_76.jpg\n",
      "(1, 4)\n",
      "[done] val_77.jpg\n",
      "(3, 4)\n",
      "[done] val_78.jpg\n",
      "(1, 4)\n",
      "[done] val_79.jpg\n",
      "(1, 4)\n",
      "[done] val_8.jpg\n",
      "(1, 4)\n",
      "[done] val_80.jpg\n",
      "(1, 4)\n",
      "[done] val_81.jpg\n",
      "(1, 4)\n",
      "[done] val_82.jpg\n",
      "(3, 4)\n",
      "[done] val_83.jpg\n",
      "(1, 4)\n",
      "[done] val_84.jpg\n",
      "(3, 4)\n",
      "[done] val_85.jpg\n",
      "(1, 4)\n",
      "[done] val_86.jpg\n",
      "(2, 4)\n",
      "[done] val_87.jpg\n",
      "(1, 4)\n",
      "[done] val_88.jpg\n",
      "(1, 4)\n",
      "[done] val_89.jpg\n",
      "(1, 4)\n",
      "[done] val_9.jpg\n",
      "(1, 4)\n",
      "[done] val_90.jpg\n",
      "(1, 4)\n",
      "[done] val_91.jpg\n",
      "(1, 4)\n",
      "[done] val_92.jpg\n",
      "(2, 4)\n",
      "[done] val_93.jpg\n",
      "(2, 4)\n",
      "[done] val_94.jpg\n",
      "(1, 4)\n",
      "[done] val_95.jpg\n",
      "(1, 4)\n",
      "[done] val_96.jpg\n",
      "(1, 4)\n",
      "[done] val_97.jpg\n",
      "(3, 4)\n",
      "[done] val_98.jpg\n",
      "(1, 4)\n",
      "[done] val_99.jpg\n",
      "Finish making detection results!\n"
     ]
    }
   ],
   "source": [
    "# making detection-results\n",
    "class mAP_Yolo(YOLO):\n",
    "    #---------------------------------------------------#\n",
    "    #   检测图片\n",
    "    #---------------------------------------------------#\n",
    "    def detect_image(self, image_id, image):\n",
    "        self.confidence = 0.05\n",
    "        image_shape = np.array(np.shape(image)[0:2])\n",
    "\n",
    "        crop_img = np.array(letterbox_image(image, (self.model_image_size[0], self.model_image_size[1])))\n",
    "        photo = np.array(crop_img, dtype = np.float32)\n",
    "        photo /= 255.0\n",
    "        photo = np.transpose(photo, (2, 0, 1))\n",
    "        photo = photo.astype(np.float32)\n",
    "        images = []\n",
    "        images.append(photo)\n",
    "        images = np.asarray(images)\n",
    "\n",
    "        with torch.no_grad():\n",
    "            images = torch.from_numpy(images)\n",
    "            if self.cuda:\n",
    "                images = images.cuda()\n",
    "            outputs = self.net(images)\n",
    "            \n",
    "        output_list = []\n",
    "        for i in range(3):\n",
    "            output_list.append(self.yolo_decodes[i](outputs[i]))\n",
    "        output = torch.cat(output_list, 1)\n",
    "        batch_detections = non_max_suppression(output, len(self.class_names),\n",
    "                                                conf_thres=self.confidence,\n",
    "                                                nms_thres=0.3)\n",
    "\n",
    "        try:\n",
    "            batch_detections = batch_detections[0].cpu().numpy()\n",
    "        except:\n",
    "            return image\n",
    "            \n",
    "        top_index = batch_detections[:,4]*batch_detections[:,5] > self.confidence\n",
    "        top_conf = batch_detections[top_index,4]*batch_detections[top_index,5]\n",
    "        top_label = np.array(batch_detections[top_index,-1],np.int32)\n",
    "        top_bboxes = np.array(batch_detections[top_index,:4])\n",
    "        top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims(top_bboxes[:,0],-1),np.expand_dims(top_bboxes[:,1],-1),np.expand_dims(top_bboxes[:,2],-1),np.expand_dims(top_bboxes[:,3],-1)\n",
    "\n",
    "        # 去掉灰条\n",
    "        boxes = yolo_correct_boxes(top_ymin,top_xmin,top_ymax,top_xmax,np.array([self.model_image_size[0],self.model_image_size[1]]),image_shape)\n",
    "\n",
    "        with open(os.path.join('mAP/input/detection-results', image_id + '.txt'), 'w+') as f:\n",
    "            for i, c in enumerate(top_label):\n",
    "                predicted_class = self.class_names[c]\n",
    "                score = str(top_conf[i])\n",
    "\n",
    "                top, left, bottom, right = boxes[i]\n",
    "                f.write(' '.join((str(predicted_class), str(score), str(left), str(top), str(right), str(bottom))) + '\\n')\n",
    "    \n",
    "\n",
    "print('Start making detection results!')\n",
    "yolo = mAP_Yolo()\n",
    "for image in images:\n",
    "    img = Image.open(os.path.join(image_path, image))\n",
    "    yolo.detect_image(image.split('.')[0], img)\n",
    "    print('[done] ' + image)\n",
    "print('Finish making detection results!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
